package org.wkg.lesson10;

import java.util.concurrent.locks.ReentrantLock;

public class Demo_1104 {
    public static void main(String[] args) throws InterruptedException {
        Counter1104 counter = new Counter1104();

        // 创建两个线程对一个变量进时累加
        Thread t1 = new Thread(() -> {
            // 5万次
            for (int i = 0; i < 50000; i++) {
                counter.increase();
            }
        });

        // 线程2
        Thread t2 = new Thread(() -> {
            // 5万次
            for (int i = 0; i < 50000; i++) {
                counter.increase();
            }
        });

        // 启动线程
        t1.start();
        t2.start();

        // 等待线程执行完成
        t1.join();
        t2.join();

        // 查看运行结果
        System.out.println("count = " + counter.count);
    }
}


class Counter1104 {
    public int count = 0;
    ReentrantLock lock = new ReentrantLock();

    public void increase() {
        try {
            lock.lock();

        } finally {
            lock.unlock();
        }
    }

/*    public void increase() {
        synchronized (this){
            count++;

        }
    }*/


}